/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package InfoMat;

import java.io.*;
import java.util.*;
import javax.microedition.io.file.*;
import javax.microedition.io.Connector;
import java.io.IOException;
import javax.microedition.lcdui.*;

/**
 *
 * @author thomas.s
 */
public class RSSMenu extends Canvas {

    InfoMat parent;
    Vector rss = new Vector();
    RSSFeed feed=null;
    int current=0;

    public class RSSFeed {

        String url, name;
        int size=1;
        boolean utf8=true, auto=false;

        public RSSFeed( String n, String u )
        {
            name = n;
            url = u;
        }

    }

    public RSSMenu( InfoMat p)
    {
        parent = p;
        setTitle("InfoMat - Feeds:");

        FileConnection fc = null;
        DataInputStream is = null;
        try {
           fc = (FileConnection)Connector.open("file:///filesystem/infomat.ini",Connector.READ);
           is = fc.openDataInputStream();
        } catch( IOException e) {}
        if ( is!=null )
        {
           char c=0;
           int index=0;
           StringBuffer url=new StringBuffer(64), name=new StringBuffer(64);
           boolean utf8=true, auto=false, eof=false;
           int size=0;

           while( !eof )
           {
               try { c=(char)is.readByte(); } catch( IOException e ) { eof=true; }
               if (c==10 || c==13 || eof)
               {
                    if (index>1)
                    {
                        feed = new RSSFeed(name.toString(),url.toString());
                        feed.auto = auto;
                        feed.utf8 = utf8;
                        feed.size = size;
                        rss.addElement( feed );
                    }
                    index = 0;
                    name = new StringBuffer(64);
                    url = new StringBuffer(64);
                    utf8 = true;
                    auto = false;
                    size = 1;
               }
               else
               {
                   if (c==';') index++; else
                   if (index==0) name.append(c); else
                   if (index==1) url.append(c); else
                   if (index>1) {
                       if (c=='a'||c=='A') auto=true; else
                       if (c=='i'||c=='I') utf8=false; else
                       if (c=='0') size=0; else
                       if (c=='1') size=1; else
                       if (c=='2') size=2; else
                       if (c=='3') size=3; else;
                   }
               }
           }
        } else
        {
            feed = new RSSFeed("WM 2010 Live","http://rss.kicker.de/live/wm");
            feed.auto = true;
            feed.utf8 = false;
            feed.size = 2;
            rss.addElement( feed );

            feed = new RSSFeed("Bundesliga Live","http://rss.kicker.de/live/bundesliga");
            feed.auto = true;
            feed.utf8 = false;
            feed.size = 2;
            rss.addElement( feed );

            feed = new RSSFeed("TV-Abendprogramm","http://www.tvmovie.de/rss/tv2015.xml");
            feed.auto = false;
            feed.utf8 = true;
            feed.size = 0;
            rss.addElement( feed );
        }

       current = 0;
       feed = (RSSFeed)(rss.elementAt(0));
    }
/*
    void next()
    {
        if ( rss.size()>0 )
        {
            current = (current+1)%(rss.size());
            feed = ((RSSFeed)rss.elementAt(current));
            parent.showFeed();
        }
        else
            feed = null;
    }
*/
    String url()  {  return feed!=null ? feed.url : "";  }
    String name()  {  return feed!=null ? feed.name : "";  }
    boolean utf8()  {  return feed!=null ? feed.utf8 : true;  }
    boolean auto()  {  return feed!=null ? feed.auto : false;  }
    int size() { return feed!=null ? feed.size : 0; }

    public void paint( Graphics g )
    {
        int w = getWidth();
        int h = getHeight();

        g.setColor(0);
        g.fillRect(0,0,w,h);

        RSSFeed f;

        for ( int i=0; i<rss.size(); i++ )
        {
            f = ((RSSFeed)rss.elementAt(i));
            g.setColor(i==current?0xf0f080:0x8080f0);
            g.drawString( f.name, 10, 10+i*50, 0 );
            g.setColor(0x303040);
            g.fillRect(0, i*50+50, w,1);
        }
    }

    public void pointerPressed( int x, int y )
    {
        int i = y/50;
        if (i<rss.size()) { current=i; repaint(); }
    }

    public void pointerDragged( int x, int y )
    {
        int i = y/50;
        if (i<rss.size()) { current=i; repaint(); }
    }

    public void pointerReleased( int x, int y )
    {
        int i = y/50;
        if (i<rss.size())
        {
            feed = ((RSSFeed)rss.elementAt((current)));
            parent.showFeed();
        }
    }
}
